package com.magic.web.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @description
 * @author wm
 * @date 2021/3/25
 */
@Slf4j
public class HolidayUtils {


    /**
     * 当年国定节假日
     */
    private static Set<String> holidaySet = new HashSet();

    /**
     * 百度日期api接口
     */
    private static String PERPETUAL_CALENDAR_URL = "http://opendata.baidu.com/api.php?query=FORMAT_DATE&resource_id=6018&format=json";

    /**
     * 放假
     */
    private static final String HOLIDAY = "1";

    /**
     * 补班
     */
    private static final String MAKE_UP = "2";

    /**
     * 日期格式化规则
     */
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");

    /**
     * 正确响应状态
     */
    private static String API_SUCCESS_STATUS = "0";

    //获取周末和节假日
    public static Set<String> JJR(int year, int month) {
        //获取所有的周末
        Set<String> monthWekDay = getMonthWekDay(year, month);
        //http://timor.tech/api/holiday api文档地址
        Map jjr = getJjr(year, month + 1);
        Integer code = (Integer) jjr.get("code");
        if (code != 0) {
            return monthWekDay;
        }
        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.get("holiday");
        Set<String> strings = holiday.keySet();
        for (String str : strings) {
            Map<String, Object> stringObjectMap = holiday.get(str);
            Integer wage = (Integer) stringObjectMap.get("wage");
            String date = (String) stringObjectMap.get("date");
            //筛选掉补班
            if (wage.equals(1)) {
                monthWekDay.remove(date);
            } else {
                monthWekDay.add(date);
            }
        }
        return monthWekDay;
    }

    //获取节假日不含周末
    private static Map getJjr(int year, int month) {
        String url = "http://timor.tech/api/holiday/year/" + year + "-" + month;
        OkHttpClient client = new OkHttpClient();
        Response response;
        //解密数据
        String rsa = null;
        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        try {
            response = client.newCall(request).execute();
            rsa = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(rsa, Map.class);
    }

    //获取周末  月从0开始
    public static Set<String> getMonthWekDay(int year, int mouth) {
        Set<String> dateList = new HashSet<>();
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, mouth, 1);
        Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
        endCalendar.add(Calendar.MONTH, 1);
        while (true) {
            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
            if (weekday == 1 || weekday == 7) {
                dateList.add(simdf.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
            if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
                break;
            }
        }
        return dateList;
    }


    public static void main(String[] args) throws Exception {
        Set<String> jjr = JJR(2021, 4);
        System.out.println(jjr);
        Map jjr1 = getJjr(2021, 4);
        System.out.println(jjr1);
        /*JSONObject requeststr = new JSONObject();
        String result = HttpUtils.sendMessageByGetWithUTF8(PERPETUAL_CALENDAR_URL);
        System.out.println(result);*/
    }


}
